/**
 * 
 */
package com.iknight.mgr.core.aop;

import java.lang.reflect.Method;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Component;

import com.iknight.mgr.core.utils.SpringEhcacheUtils;

/**
 * @author Administrator
 *
 */
@Aspect
@Component
public class EhcacheShiroAspect {
	
	private final Logger logger = LogManager.getLogger(EhcacheShiroAspect.class);
	private String authorizeName = "shiroAuthorizationCache";

	public String getAuthorizeName() {
		return authorizeName;
	}

	public void setAuthorizeName(String authorizeName) {
		this.authorizeName = authorizeName;
	}
	@Autowired
	private SpringEhcacheUtils ehcacheUtils;

	@Pointcut("@annotation(com.iknight.mgr.core.annotation.ShiroEffectAnnotation)")
	public void beforeAspect() {
		logger.debug("开启测试");
	}

	@Before("beforeAspect()")
	public void before(JoinPoint point) {
		/**
		 * 识别调用对象，由哪个类，哪个方法 本例中实现对于资源授权，密码变更，资源、用户两个类的切面管理 修改指定的用户
		 */
		logger.debug("之前执行");
		Object target = point.getTarget();
		String method = point.getSignature().getName();
		Class<?>[] classz = target.getClass().getInterfaces();
		Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();

		try {
			Method m = classz[0].getMethod(method, parameterTypes);
			logger.debug(m.getName());
			Object[] args = point.getArgs();
			if (null == m || null == args) {// 判断方法是否存在，参数是否存在
				return;
			}
			logger.debug(ehcacheUtils.getCache(authorizeName));
			Cache cache = ehcacheUtils.getCache(authorizeName);
			logger.debug(cache.getName());
			logger.debug(cache.toString());
//			ehcacheUtils.remove(cacheName, key);
		} catch (NoSuchMethodException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
